package org.zpmis.api.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zpmis.api.business.ContractWebService;
import org.zpmis.common.base.BaseController;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.common.result.ResultCode;
import org.zpmis.domain.contract.qo.AddContractQo;
import org.zpmis.domain.contract.qo.ContractDetailQo;
import org.zpmis.domain.contract.qo.ContractListQo;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description: 合同处理接口
* @author  Yang
* date: 2021-6-2
* time: 14:52:19
*/
@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController extends BaseController{

    @Resource
    private ContractWebService contractWebService;


    /**
     * 新增合同
     * @param addContractQo
     * @return
     */
    @PostMapping("/addContract")
    public PlatformResult addContract(@Validated @RequestBody AddContractQo addContractQo){

        try {
            contractWebService.addContract(addContractQo);
            return PlatformResult.success();
        } catch (BusinessException be) {
            log.error("[ContractController]---addContract--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[ContractController]---addContract--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 合同列表查询
     * @param contractListQo
     * @return
     */
    @PostMapping("/contractList")
    public PlatformResult contractList(@RequestBody ContractListQo contractListQo){
        try {
            return PlatformResult.success(contractWebService.contractList(contractListQo));
        } catch (BusinessException be) {
            log.error("[ContractController]---contractList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[ContractController]---contractList--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 查询合同详情
     * @param contractDetailQo
     * @return
     */
    @PostMapping("/contractDetail")
    public PlatformResult contractDetail(@Validated @RequestBody ContractDetailQo contractDetailQo){

        try {
            return PlatformResult.success(contractWebService.contractDetail(contractDetailQo));
        } catch (BusinessException be) {
            log.error("[ContractController]---contractDetail--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[ContractController]---contractDetail--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 判断当前小程序登录用户是否签订入网合同
     * 校验能否申请订单
     * @param
     * @return
     */
    @PostMapping("/checkOrderContract")
    public PlatformResult checkOrderContract(){
        try {
            return PlatformResult.success(contractWebService.checkOrderContract());
        } catch (BusinessException e) {
            log.error("[OrderController]---checkOrderContract--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[OrderController]---checkOrderContract--->Exception:",e);
            return PlatformResult.failure(ResultCode.ERROR);
        }
    }

    /**
     * 删除合同
     * @param contractDetailQo
     * @return
     */
    @PostMapping("/deleteContract")
    public PlatformResult deleteContract(@Validated @RequestBody ContractDetailQo contractDetailQo){

        try {
            contractWebService.deleteContract(contractDetailQo);
            return PlatformResult.success();
        } catch (BusinessException be) {
            log.error("[ContractController]---deleteContract--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[ContractController]---deleteContract--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
